package com.zyzy.leftfourdead.activity;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import com.zyzy.leftfourdead.R;
import com.zyzy.leftfourdead.fragment.HomePageFragment;
import com.zyzy.leftfourdead.fragment.groupbuy.GroupBuyPagerFragment;
import com.zyzy.leftfourdead.helper.FragmentHelper;

import java.util.ArrayList;
import java.util.List;
// 不好使了呢
public class MainActivity extends AppCompatActivity {
        //创建上下文对象
        private Context context=this;
        //创建RadioGroup对象
        private RadioGroup radioGroup_main;
        //创建碎片管理器
        private FragmentManager manager;
        //创建装载碎片的集合
        private List<Fragment> totalList=new ArrayList<>();
        //创建帧布局对象
        private FrameLayout frameLayout_main;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
        }

    private void initView() {
        //初始化RadioGroup对象
        radioGroup_main= (RadioGroup) findViewById(R.id.radioGroup_main);
        //初始化碎片管理器
        manager=getSupportFragmentManager();
        //将HomePageFragment大碎片添加到集合中
        HomePageFragment homePageFragment = new HomePageFragment();
        homePageFragment.setManager(manager);
        totalList.add(homePageFragment);
        totalList.add(new GroupBuyPagerFragment(manager));
//        //创建事务
//        FragmentTransaction transaction = manager.beginTransaction();
//        transaction.replace()
        //设置默认为第一个碎片
        radioGroup_main.check(radioGroup_main.getChildAt(0).getId());
        //第一个参数 碎片管理器  第二个参数 碎片集合 第三个参数 默认的下标 第四个参数 要填充的布局
        FragmentHelper.replaceFragment(manager,totalList,0,R.id.frameLayout_main);
        radioGroup_main.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for (int i = 0; i < totalList.size(); i++) {
                    if(group.getChildAt(i).getId() == checkedId){
                        FragmentHelper.switchFragment(manager,totalList,i,R.id.frameLayout_main);
                    }
                }
            }
        });
    }
}
